package com.fas.hehe.system.controller;

import java.util.HashMap;
import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.fas.core.BaseController;
import com.fas.core.constant.R;
import com.fas.core.plugin.jfinal.route.ControllerBind;
import com.fas.core.plugin.tree.jstree.Node;
import com.fas.hehe.system.model.Resource;
import com.fas.hehe.system.service.ResourceService;
import com.jfinal.aop.Before;
import com.jfinal.plugin.ehcache.CacheName;
import com.jfinal.plugin.ehcache.EvictInterceptor;

/**
 * 资源管理 菜单、按钮定义
 * 
 * 
 * TODO 页面表单校验 、状态过滤、 页面操作按校验
 * 
 * @Copyright: Copyright (c) 2016 FAS
 * @anthor yixiaoping<thinker1013@163.com>
 * @since 2016年1月24日 上午11:21:09
 * @version V1.0
 */
@RequiresAuthentication
@ControllerBind(controllerKey = "/system/resource", viewPath = "/system/resource")
public class ResourceController extends BaseController {

	public ResourceService resourceService;

	// 一次性加载字典分类树
	@RequiresPermissions(value = { R.Authority.Res.QUERY }, logical = Logical.AND)
	public void loadDictionaryTreeData() {
		List<Node> treeNodes = resourceService.loadTreeOnceTime();
		HashMap<String, Object> returnData = new HashMap<String, Object>();
		returnData.put("treeNodes", treeNodes);

		this.returnJsonResult(returnData);
	}

	// 加载资源下面信息（包含资源本身信息、子资源列表）
	@RequiresPermissions(value = { R.Authority.Res.QUERY }, logical = Logical.AND)
	public void loadResourceInfoById() {
		Resource resource = (Resource) this.findById(this.getParaToLong("resId"));
		List<Resource> subResources = resourceService.findSubResourceById(this.getParaToLong("resId"));

		HashMap<String, Object> returnData = new HashMap<String, Object>();
		returnData.put("resource", resource);
		returnData.put("subResources", subResources);

		this.returnJsonResult(returnData);
	}

	// 修改资源优先级别(显示顺序 )
	@RequiresPermissions(value = { R.Authority.Res.UPDATE }, logical = Logical.AND)
	@Before(EvictInterceptor.class)
	@CacheName(R.CacheName.GLOBAL_RES_DATA)
	public void changeResourcePriority() {
		String resId = this.getPara("resId");
		String type = this.getPara("type");
		int orderby = this.getParaToInt("priority");
		boolean isSuccess = resourceService.changeResourcePriority(resId, type, orderby);
		this.returnJsonResult(isSuccess);
	}

	// 删除资源
	@RequiresPermissions(value = { R.Authority.Res.DELETE }, logical = Logical.AND)
	@Before(EvictInterceptor.class)
	@CacheName(R.CacheName.GLOBAL_RES_DATA)
	public void deleteResource() {
		boolean isSuccess = resourceService.deleteResourceById(this.getParaToLong("id"));
		this.returnJsonResult(isSuccess);
	}

	// 保存资源(for create OR update)
	@RequiresPermissions(value = { R.Authority.Res.CREATE, R.Authority.Res.UPDATE }, logical = Logical.OR)
	@Before(EvictInterceptor.class)
	@CacheName(R.CacheName.GLOBAL_RES_DATA)
	public void saveResource() throws Exception {
		Resource resource = (Resource) this.getModelEntity();
		resource = resourceService.saveResource(resource);
		Node node = resourceService.buildJstreeNode(resource);
		this.returnJsonResult(node);
	}

	// 根据ID查询资源对象
	@RequiresPermissions(value = { R.Authority.Res.QUERY }, logical = Logical.AND)
	public void findResourceById() {
		Resource resource = (Resource) this.findById(this.getParaToLong("id"));
		this.returnJsonResult(resource);
	}

}
